/**
 * 质控现场考核管理模块
 */
(function(window, angular, undefined){
	var module = angular.module('water.gs.operationManager.serversPolling.serversPollingManager',['water.entity.serversPolling','ui.router','itfreer','waterCommon']);
	
	module.controller('serversPollingManagerController',function($rootScope,$wdp,$httpData,$scope,$waterDataCommon,$serversPollingService,$controller,OperationStationJobService,UploadService){
		/**
		 * 新增对话框
		 */
		$scope.serversPollingAddDialog;
		/**
		 * 上传对话框对象
		 */
		$scope.uploadDialog;
		/**
		 * 详情对话框
		 */
		$scope.serversPollingDetailDialog;
		/**
		 * 当前登录用户信息
		 */
		$scope.USER={};
		/**
		 * 查询按钮单击事件
		 */
		$scope.ongetListClick=function(){
			_getList();
		}
		/**
		 * 列表页面参数集
		 */
		$scope.queryObject={};
		/**
		 * 新增页面参数集
		 */
		$scope.object={};
		/**
		 * 新增按钮点击事件
		 */
		$scope.onAddClick = function(){	
			defaultParmas();
			$scope.serversPollingAddDialog.open();
		}
		/**
		 *  详情按钮点击事件
		 */
		$scope.ondetailClick = function(data){			
			$scope.serversPolling=data;
			$scope.serversPollingDetailDialog.open();
		}
		/**
		 * 保存按钮
		 */
		$scope.onSubmitCreateClick=function(object){			
			_createModel(parmas(object));
		 }
		/**
		 * 关闭新增窗口按钮
		 */
		$scope.onCreateCloseClick = function(){	
			$scope.object={};
			$scope.serversPollingAddDialog.close();
		}
		/**
		 * 获取服务器巡检列表
		 */
		function _getList(){	
			var date=new Date();
			//默认时间查询
			if($scope.queryObject.start==undefined||$scope.queryObject.start==null){
				$scope.queryObject.end=date.getFullYear() + "-"+ (date.getMonth()+1) + "-" + date.getDate();
				$scope.queryObject.start=date.getFullYear() + "-"+ (date.getMonth()-2) + "-" + date.getDate();
			}
			$serversPollingService.getList($scope.queryObject).then(function(d){
				$scope.serversPollingList = d;
			});
		}
		/**
		 * 查询文件
		 */
		$scope.queryFile = function(){
			var date=new Date();
			//默认时间查询
			if($scope.queryObject.start==undefined||$scope.queryObject.start==null){
				$scope.queryObject.end=date.getFullYear() + "-"+ (date.getMonth()+1) + "-" + date.getDate();
				$scope.queryObject.start=date.getFullYear() + "-"+ (date.getMonth()-2) + "-" + date.getDate();
			}
			$waterDataCommon.getPageRows("/ServerspollingController/getFileList.do","fileDataList",$scope.queryObject,10,1);
		};
		/**
		 * 查询报告
		 */
		$scope.queryReport = function(){
			var date=new Date();
			//默认时间查询
			if($scope.queryObject.start==undefined||$scope.queryObject.start==null){
				$scope.queryObject.end=date.getFullYear() + "-"+ (date.getMonth()+1) + "-" + date.getDate();
				$scope.queryObject.start=date.getFullYear() + "-"+ (date.getMonth()-2) + "-" + date.getDate();
			}
			$waterDataCommon.getPageRows("/ServerspollingController/getReportList.do","reportDataList",$scope.queryObject,10,1);
		};
		/**
		 * 导出
		 */
		$scope.onToPdfClick=function(data){		
			$serversPollingService.ToPdf({GUID:data.GUID});
		}
		/**
		 * 上传文件
		 */
		$scope.uploadFile = function(){
			UploadService.upload(uploadSuccessHandler);
		};
		$scope.fileList = [];
		function uploadSuccessHandler(fileInfo){
			$scope.fileList.push({"S_URL":fileInfo.file});
			$scope.$apply();
		}
		/**
		 * 保存文件
		 */
		$scope.saveFile = function(){
			if($scope.fileList.length>0){
			var saveObj = {};
					var list = [];
					angular.forEach($scope.fileList,function(value,key){
						list.push(value.S_URL);
					});
					if(list.length){
						saveObj.FILES = list.toString();
						OperationStationJobService.postQuery("/ServerspollingController/saveFileList.do",saveObj).then(function(r){
							$itfreer.closeWindow("serversPollingAddDialog");
							$.messager.alert("提示","保存成功");
							$scope.serversPollingAddDialog.close();
							$scope.queryFile();
			            });
					}
		   }
		};
		/**
		 * 保存报告
		 */
		$scope.saveReport = function(){
			if($scope.fileList.length>0){
			var saveObj = {};
					var list = [];
					angular.forEach($scope.fileList,function(value,key){
						list.push(value.S_URL);
					});
					if(list.length){
						saveObj.FILES = list.toString();
						OperationStationJobService.postQuery("/ServerspollingController/saveReportList.do",saveObj).then(function(r){
							$itfreer.closeWindow("serversPollingAddDialog");
							$.messager.alert("提示","保存成功");
							$scope.serversPollingAddDialog.close();
							$scope.queryReport();
			            });
					}
		   }
		};
		/**
		 * 下载文件
		 */
		$scope.downloadFile = function(curRow){
			var fileInfo = curRow.S_URL;
			if (!fileInfo) return;
			fileInfoArr = fileInfo.split(":");
			var url = fileInfoArr[0];
			var name = curRow.S_NAME;
			var downloadUrl = getBasePath()+'/plupload/download.do';
			var _downloadUrl = downloadUrl+"?filePath="+url.replace(getBasePath()+"/","")+"&fileName="+encodeURIComponent(name);
			window.open(_downloadUrl);
		};
		/**
		 * 删除
		 */
		$scope.deleteFile = function(Row){
			var queryObj = {};
			queryObj.GUID = Row.GUID;
			OperationStationJobService.postQuery("/ServerspollingController/deleteFile.do",queryObj).then(function(r){
				$itfreer.closeWindow("serversPollingAddDialog");
				$.messager.alert("提示","删除成功");
				$scope.serversPollingAddDialog.close();
				$scope.queryReport();
            });
		}
		/**
		 * 上传页面提交按钮点击事件
		 */
		$scope.onSubmitUploadClick = function(){
			$dataComparisonService.updateComparisonSummaryModel($scope.dataComparisonUpload).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.getDataComparisonSummaryList();
				$scope.uploadDialog.close();
			});
		};
		
		/**
		 * 上传按钮点击事件，用于图片上传
		 */
		$scope.onUploadClick = function(curRow){
			$scope.dataComparisonUpload = angular.copy(curRow);
			$scope.uploadDialog.open();
		}
		/**
		 * 新增服务器巡检
		 */
		function _createModel(data){
			$serversPollingService.createModel(data).then(function(){
				$.messager.alert("保存成功","保存成功",'info');
				$scope.serversPollingAddDialog.close();
				$scope.object={};
				_getList();
			});
		}
		function checkErr(){		   
		    if((new Date($scope.queryObject.start.replace(/-/g,"\/"))) > (new Date($scope.queryObject.end.replace(/-/g,"\/")))){
		    	$.messager.alert("提示","起始时间不能大于结束时间");
		      return false;
		    }		 
		  };
		  /**
		   * 参数拼接
		   */
		function parmas(data){
			$scope.parmas={};
			$scope.parmas.TEMPERATURE=data.TEMPERATURE1+parmasIsEmpty(data.TEMPERATURE1_NOTE);//温度
			$scope.parmas.HUMIDITY=data.HUMIDITY1+parmasIsEmpty(data.HUMIDITY1_NOTE);//湿度
			$scope.parmas.VESTIGE=data.VESTIGE1+parmasIsEmpty(data.VESTIGE1_NOTE);//痕迹
			$scope.parmas.CLEAN=data.CLEAN1+parmasIsEmpty(data.CLEAN1_NOTE);//清洁
			$scope.parmas.NOISE=data.NOISE1+parmasIsEmpty(data.NOISE1_NOTE);//异响
			$scope.parmas.RAREDELICACY=data.RAREDELICACY1+parmasIsEmpty(data.RAREDELICACY1_NOTE);//异味
			$scope.parmas.NETWORK=data.NETWORK1+parmasIsEmpty(data.NETWORK1_NOTE);//网络通讯状况
			$scope.parmas.PILOTLAMP=data.PILOTLAMP1+parmasIsEmpty(data.PILOTLAMP1_NOTE);//指示灯
			$scope.parmas.INTERCHANGER=data.INTERCHANGER1+parmasIsEmpty(data.INTERCHANGER1_NOTE);//交换机
			$scope.parmas.TROUBLELIGHT=data.TROUBLELIGHT1+parmasIsEmpty(data.TROUBLELIGHT1_NOTE);//故障灯
			$scope.parmas.DEDUSTING=data.DEDUSTING1+parmasIsEmpty(data.DEDUSTING1_NOTE);//除尘
			$scope.parmas.SYSTEMOPERATION=data.SYSTEMOPERATION1+parmasIsEmpty(data.SYSTEMOPERATION1_NOTE);//供水管网系统运行情况
			$scope.parmas.DATABASE=data.DATABASE1+parmasIsEmpty(data.DATABASE1_NOTE);//检查数据库
			$scope.parmas.SERVERSPATCH=data.SERVERSPATCH1+parmasIsEmpty(data.SERVERSPATCH1_NOTE);//服务器补丁检查更新
			$scope.parmas.USELESSFILECLEAR=data.USELESSFILECLEAR1+parmasIsEmpty(data.USELESSFILECLEAR1_NOTE);//垃圾文件清理
			$scope.parmas.VIRUSINSPECT=data.VIRUSINSPECT1+parmasIsEmpty(data.VIRUSINSPECT1_NOTE);//病毒检查、杀毒
			$scope.parmas.VIRUSRESERVOIR=data.VIRUSRESERVOIR1+parmasIsEmpty(data.VIRUSRESERVOIR1_NOTE);//病毒库检查更新
			$scope.parmas.LOOPHOLEINSPECT=data.LOOPHOLEINSPECT1+parmasIsEmpty(data.LOOPHOLEINSPECT1_NOTE);//漏洞检查更新
			$scope.parmas.SYSTEMLOG=data.SYSTEMLOG1+parmasIsEmpty(data.SYSTEMLOG1_NOTE);//系统日志
			$scope.parmas.DISKINSPECT=data.DISKINSPECT1+parmasIsEmpty(data.DISKINSPECT1_NOTE);//磁盘检查
			$scope.parmas.DATABACKUP=data.DATABACKUP1+parmasIsEmpty(data.DATABACKUP1_NOTE);//数据库备份
			$scope.parmas.POLLING_USER=$scope.USER.S_USERID;//巡检人
			$scope.parmas.RESTSISSUE=data.RESTSISSUE;//其他
			return $scope.parmas;
		}
		/**
		 * 设置默认值
		 */
		function defaultParmas(){
			$scope.object.TEMPERATURE1=1;//温度
			$scope.object.HUMIDITY1=1;//湿度
			$scope.object.VESTIGE1=1;//痕迹
			$scope.object.CLEAN1=1;//清洁
			$scope.object.NOISE1=1;//异响
			$scope.object.RAREDELICACY1=1;//异味
			$scope.object.NETWORK1=1;//网络通讯状况
			$scope.object.PILOTLAMP1=1;//指示灯
			$scope.object.INTERCHANGER1=1;//交换机
			$scope.object.TROUBLELIGHT1=1;//故障灯
			$scope.object.DEDUSTING1=1;//除尘
			$scope.object.SYSTEMOPERATION1=1;//供水管网系统运行情况
			$scope.object.DATABASE1=1;//检查数据库
			$scope.object.SERVERSPATCH1=1;//服务器补丁检查更新
			$scope.object.USELESSFILECLEAR1=1;//垃圾文件清理
			$scope.object.VIRUSINSPECT1=1;//病毒检查、杀毒
			$scope.object.VIRUSRESERVOIR1=1;//病毒库检查更新
			$scope.object.LOOPHOLEINSPECT1=1;//漏洞检查更新
			$scope.object.SYSTEMLOG1=1;//系统日志
			$scope.object.DISKINSPECT1=1;//磁盘检查
			$scope.object.DATABACKUP1=1;//数据库备份
			return $scope.parmas;
		}
		function parmasIsEmpty(object){
			if(object==undefined){
				return "";
			}else{
				return object;
			}
		}
		//获取当前登录用户
		OperationStationJobService.postQuery("/Data/getCurrentUser.do",{}).then(function(data){
			if(data){
				$scope.USER=data;
			}
		});
		  _getList();
		  $scope.queryReport();
		  $scope.queryFile();
	});	
}(window, angular));	